package CutLoad.web.menu
{
	import CutLoad.Base;
	import CutLoad.components.CButton;
	import CutLoad.components.container.ContainerH;
	import CutLoad.components.container.ContainerV;
	import CutLoad.cookies.MouseInOutThisRect;
	import CutLoad.transitions.UpDownAndAlphaInOut;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * 横向菜单，样式1
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 上午09:58:46 
	 *
	 */
	public class MenuHorizontal_1 extends Base
	{
		private var menuContainer:ContainerH;
		private var getMenuItem:Function,getMenuSubItem:Function;
		private var arrData:MenuData2;
		//当前over的位置
		private var currentMouseOverIndex:Number=-1;
		private var currentMenu:Base;
		/**
		 * 初始化 
		 * @param data 二级菜单数据
		 * @param getMenuItem  从这个函数里获得一级菜单显示对象。 格式如下： function(index:Number,label:String):Base
		 * @param getMenuSubItem  从这个函数里获得2级菜单显示对象。 格式如下： function(index:Number,arr:Array):Base
		 * 
		 */		
		public function MenuHorizontal_1(data:MenuData2,getMenuItem:Function=null,getMenuSubItem:Function=null)
		{
			menuContainer=ContainerH.getSimple();
			if(getMenuItem==null) getMenuItem=default_getMenuItem;
			this.getMenuItem=getMenuItem;
			if(getMenuSubItem==null) getMenuSubItem=default_getMenuSubItem;
			this.getMenuSubItem=getMenuSubItem;
			this.arrData=data;
			//
			update()
			menuContainer.show(this);
		}
		//更新一级菜单
		protected function update():void{
			menuContainer.removeAll();
			var arr:Array=arrData.data1;
			for(var i:Number=0;i<arr.length;i++){
				var btnTem:Base=getMenuItem(i,arr[i]);
				btnTem.addEventListener(MouseEvent.MOUSE_OVER,menu1_mouseEvent);
				menuContainer.append(btnTem,i==arr.length-1?true:false);
			}
			menuContainer.setTakeWH();
		}
		//生成一级菜单元素的默认方法
		protected function default_getMenuItem(index:Number,label:String):Base{
			//trace("default_getMenuItem:"+label+" "+data);
			var btn:CButton=CButton.getSimpleBtn(label);
			btn.setOtherInfo({MenuHorizontal_1_MenuItem_data:index});
			return btn;
		}
		//生成2级菜单元素的默认方法
		protected function default_getMenuSubItem(index:Number,arr:Array):Base{
			var containerTem:ContainerV=ContainerV.getSimple();
			for(var i:Number=0;i<arr.length;i++){
				var btn:CButton=CButton.getSimpleBtn(arr[i]);
				btn.setOtherInfo({MenuHorizontal_1_MenuSubItem:i,MenuHorizontal_1_MenuItem_data:index});
				containerTem.append(btn,i==arr.length-1?true:false);
			}
			containerTem.setTakeWH();
			//containerTem.showGuide();
			return containerTem;
		}
		//一级菜单鼠标事件
		private function menu1_mouseEvent(e:MouseEvent):void{
			var target:Base=e.target as Base;
			if(e.type==MouseEvent.MOUSE_OVER){
				//生成二级菜单
				var indexTem:Number=target.getOtherInfo().MenuHorizontal_1_MenuItem_data;
				if(currentMouseOverIndex==indexTem) return;
				if(currentMenu!=null){
					subMendEaseOut(currentMenu);
				}
				currentMenu=target
				currentMouseOverIndex=indexTem
				//
				var subMenu:Base=getMenuSubItem(indexTem,this.arrData.data2[indexTem]);
				//如果放到其它父类上，清除它，应该放在最上层
				if(subMenu.parent!=null) target.parent.removeChild(subMenu);
				var pTem:Point=target.parent.localToGlobal(target.getPosition());
				subMenu.x=pTem.x;
				subMenu.y=pTem.y+target.getShowHeight();
				target.stage.addChild(subMenu);
				//上下淡入淡出动画
				var movieUpDown:UpDownAndAlphaInOut=new UpDownAndAlphaInOut(subMenu);
				//保存这个动画，当鼠标移开时，让它淡出
				target.getOtherInfo().menu1_mouseEvent_movieUpDown=movieUpDown;
				var rectTem:Rectangle=subMenu.getRectInfo();
				rectTem.y-=target.getShowHeight();
				rectTem.height+=target.getShowHeight();
				movieUpDown.easeIn();
				//检查范围
				var mouseOverOutRect:MouseInOutThisRect=new MouseInOutThisRect(subMenu.parent,rectTem,function(me:MouseInOutThisRect):void{
					if(me.isIn()){
						
					}else{
						subMendEaseOut(target);
					}
				});
				//trace("target:"+target.getShowWidth()+","+target.getShowHeight())
				//trace("subMenu:"+subMenu.getShowWidth()+","+subMenu.getShowHeight())
				//trace("mouseOverOutRect.rect:"+mouseOverOutRect.rect)
				target.getOtherInfo().menu1_mouseEvent_mouseOverOutRect=mouseOverOutRect;
			}
		}
		private function subMendEaseOut(target:Base):void{
			currentMouseOverIndex=-1;
			//复杂上面清除的代码。
			if(target.getOtherInfo().menu1_mouseEvent_movieUpDown==null) return;
			var movieUpDown_tem:UpDownAndAlphaInOut=target.getOtherInfo().menu1_mouseEvent_movieUpDown as UpDownAndAlphaInOut;
			movieUpDown_tem.easeOut();
			if(movieUpDown_tem.getTarget().parent!=null) movieUpDown_tem.getTarget().parent.removeChild(movieUpDown_tem.getTarget());
			target.getOtherInfo().menu1_mouseEvent_mouseOverOutRect.killMe();
		}
	}
}